package com.qzyh.esb.egsa.utils;


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.lang.Nullable;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class ResultObject<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    static SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    public static final String DEFAULT_SUCCESS_CODE = "000000";

    public static final String DEFAULT_SUCCESS_MSG = "SUCCESS";

    public static final String DEFAULT_FAIL_CODE = "999999";

    public static final String DEFAULT_FAIL_MSG = "FAIL";

    public static final String DEFAULT_ERROR_CODE = "444444";

    public static final String DEFAULT_ERROR_MSG = "系统繁忙！请稍后再试";

    public static final String TOKEN_INVALID_CODE = "000401";

    public static final String TOKEN_INVALID_MSG = "token失效，请重新登录。";

    public static final String LOGIN_FAIL_CODE = "000402";

    public static final String LOGIN_FAIL_MSG = "账号或用户名错误，请重新登录。";


    /**
     * 时间戳
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String timestamp;
    /**
     * 响应状态
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String code;
    /**
     * 响应描述
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;
    /**
     * 响应数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;


    public ResultObject() {

    }

    /**
     * 若没有数据返回，可以人为指定状态码和提示信息
     */
    public ResultObject(String code, String message) {
        this.timestamp = sdf.format(new Date());
        this.code = code;
        this.message = message;
    }

    /**
     * 有数据返回，且需要人为指定状态码和提示信息
     */
    public ResultObject(String code, String message, T data) {
        this.timestamp = sdf.format(new Date());
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 有数据返回时，状态码为200，默认提示信息为SUCCESS
     */
    public ResultObject(T data) {
        this.data = data;
        this.code = DEFAULT_SUCCESS_CODE;
        this.message = DEFAULT_SUCCESS_MSG;
        this.timestamp = sdf.format(new Date());
    }

    /**
     * 有数据返回，状态码为 200，人为指定提示信息
     */
    public ResultObject(String msg, T data) {
        this.data = data;
        this.code = DEFAULT_SUCCESS_CODE;
        this.message = msg;
        this.timestamp = sdf.format(new Date());
    }


    public static <T> ResultObject<T> success() {
        return new ResultObject<>();
    }

    public static <T> ResultObject<T> success(String message) {
        return new ResultObject<>(DEFAULT_SUCCESS_CODE, message);
    }

    public static <T> ResultObject<T> success(T data) {
        return new ResultObject<>(data);
    }

    public static <T> ResultObject<T> success(String msg, T data) {
        return new ResultObject<>(msg, data);
    }

    public static <T> ResultObject<T> fail() {
        return new ResultObject<>(DEFAULT_FAIL_CODE, DEFAULT_FAIL_MSG);
    }

    public static <T> ResultObject<T> loginFail() {
        return new ResultObject<>(LOGIN_FAIL_CODE, LOGIN_FAIL_MSG);
    }

    public static <T> ResultObject<T> fail(String message) {
        return new ResultObject<>(DEFAULT_FAIL_CODE, message);
    }

    public static <T> ResultObject<T> error() {
        return new ResultObject<>(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MSG);
    }

    public static <T> ResultObject<T> invalid() {
        return new ResultObject<>(TOKEN_INVALID_CODE, TOKEN_INVALID_MSG);
    }
}
